package com.ecy.quadratic.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.Community;
import com.ecy.quadratic.photopicker.PhotoPreview;
import com.ecy.quadratic.reader.utils.FormatUtils;
import com.ecy.quadratic.reader.utils.ScreenUtils;
import com.ecy.quadratic.reader.view.TagColor;
import com.ecy.quadratic.reader.view.TagGroup;
import com.ecy.quadratic.ui.activity.CommunityDetailActivity;
import com.ecy.quadratic.widget.ScrollGridView;
import com.yuyh.easyadapter.glide.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

public class SpecialAreaCommunityAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<Community> list;
    private int wh;
    private Activity mActivity;

    public SpecialAreaCommunityAdapter(Activity activity, List<Community> list) {
        super();
        this.mInflater = LayoutInflater.from(activity);
        this.mContext = activity;
        mActivity = activity;
        this.wh = (ScreenUtils.getScreenWidth() - ScreenUtils.dpToPxInt(36)) / 3;
        this.list = list;
    }

    public List<Community> getList() {
        return list;
    }

    public void setList(List<Community> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return list == null ? null : list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return list == null ? null : arg0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (list.size() == 0) {
            return null;
        }
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_community, null);
            holder = new ViewHolder();
            holder.userPhotoIV = (ImageView) convertView.findViewById(R.id.community_user_portrait_iv);
            holder.nicknameTV = (TextView) convertView.findViewById(R.id.community_user_nickname_tv);
            holder.timeTV = (TextView) convertView.findViewById(R.id.community_item_time_tv);
            holder.contentTV = (TextView) convertView.findViewById(R.id.community_item_title_tv);
            holder.imagesGV = (ScrollGridView) convertView.findViewById(R.id.community_gridview);
            holder.tagGroup = (TagGroup) convertView.findViewById(R.id.tag_group);
            holder.bindAdapter();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Community bean = list.get(position);

        String name = null, time = null, content = null, headpath = null;
        ArrayList<String> contentimages = null;
        if (bean != null) {
            if (bean.userBean != null) {
                name = bean.userBean.nickname;
                headpath = bean.userBean.portrait;
            }
            time = bean.createtime;
            content = bean.content;
            if (bean.imagesBean != null) {
                contentimages = bean.imagesBean;
            }
        }
        if (bean.tagsBean != null) {
            List<String> batch = bean.tagsBean;
            if (bean.tagsBean.size() > 4) {
                batch = bean.tagsBean.subList(0, 4);
            }

            List<TagColor> colors = TagColor.getRandomColors(batch.size());
            holder.tagGroup.setTags(colors, (String[]) batch.toArray(new String[batch.size()]));
            holder.tagGroup.setVisibility(View.VISIBLE);
        } else {
            holder.tagGroup.setVisibility(View.GONE);
        }


        if (!TextUtils.isEmpty(name.trim())) {
            holder.nicknameTV.setText(name);
            holder.nicknameTV.setVisibility(View.VISIBLE);
        } else {
            holder.nicknameTV.setVisibility(View.GONE);
        }

        if (contentimages != null) {
            holder.imagesGV.setVisibility(View.VISIBLE);
            initInfoImages(holder.imagesGV, holder.mAdapter, contentimages);
        } else {
            holder.imagesGV.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(time)) {
            holder.timeTV.setText(FormatUtils.getDescriptionTimeFromDateString(time));
        }

        if (!TextUtils.isEmpty(content)) {
            holder.contentTV.setText(content);
            Linkify.addLinks(holder.contentTV, Linkify.WEB_URLS);
            holder.contentTV.setVisibility(View.VISIBLE);
        } else {
            holder.contentTV.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(headpath)) {
            holder.userPhotoIV.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(headpath).placeholder(R.drawable.cover_default_square).transform(new GlideCircleTransform(mContext)).into(holder.userPhotoIV);
        } else {
            holder.userPhotoIV.setVisibility(View.GONE);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommunityDetailActivity.startActivity(mContext, bean);
            }
        });
        return convertView;
    }

    private void initInfoImages(ScrollGridView imageGridView, final CommunityGridViewAdapter adapter, final ArrayList<String> imgspaths) {
        int itemWidth = wh;
        int itemHeight = wh;
        int gridViewWidth = 0;
        switch (imgspaths.size()) {
            case 1:
                gridViewWidth = 3 * wh + ScreenUtils.dpToPxInt(5) * 2;
                imageGridView.setNumColumns(1);
                itemWidth = gridViewWidth;
                break;
            case 2:
            case 4:
                gridViewWidth = 2 * wh + ScreenUtils.dpToPxInt(5);
                imageGridView.setNumColumns(2);
                break;
            case 3:
            case 5:
            case 6:
                gridViewWidth = wh * 3 + ScreenUtils.dpToPxInt(5) * 2;
                imageGridView.setNumColumns(3);
                break;
            default:
                gridViewWidth = wh * 3 + ScreenUtils.dpToPxInt(5) * 2;
                imageGridView.setNumColumns(3);
                break;

        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(gridViewWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageGridView.setLayoutParams(lp);
        adapter.updateData(imgspaths, itemWidth, itemHeight);
        imageGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                PhotoPreview.builder()
                        .setPhotos(imgspaths)
                        .setCurrentItem(arg2)
                        .start(mActivity);
//					CommunityDetailActivity.startActivity(mContext,adapter.getItem(arg2));
//					Toast.makeText(mContext, "选择了"+adapter.getItem(arg2)+"图片", Toast.LENGTH_LONG).show();
//					Toast.makeText(mContext, "选择了"+(arg2+1)+"图片", Toast.LENGTH_LONG).show();
            }
        });


    }

    class ViewHolder {
        ImageView userPhotoIV;
        TextView nicknameTV;
        TextView timeTV;
        TextView contentTV;
        ScrollGridView imagesGV;
        TagGroup tagGroup;
        CommunityGridViewAdapter mAdapter;

        public void bindAdapter() {
            mAdapter = new CommunityGridViewAdapter(mContext);
            imagesGV.setAdapter(mAdapter);
        }
    }


}
